package com.gitee.qdbp.general.mgr.controller.personnel;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.jdbc.ordering.OrderPaging;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.able.result.ResponseMessage;
import com.gitee.qdbp.base.system.model.IAccount;
import com.gitee.qdbp.base.utils.SessionTools;
import com.gitee.qdbp.general.system.api.personnel.model.DepartmentCoreBean;
import com.gitee.qdbp.general.system.api.personnel.model.DepartmentCoreUpdate;
import com.gitee.qdbp.general.system.api.personnel.model.DepartmentCoreWhere;
import com.gitee.qdbp.general.system.api.personnel.service.IDepartmentCoreExecutor;
import com.gitee.qdbp.general.system.api.personnel.service.IDepartmentCoreQueryer;
import com.gitee.qdbp.tools.utils.ConvertTools;

/**
 * 部门基本信息控制器
 *
 * @author zhaohuihua
 * @version 170401
 */
@Controller
@RequestMapping("actions/department")
public class DepartmentController {

    @Autowired
    private IDepartmentCoreQueryer departmentCoreQueryer;

    @Autowired
    private IDepartmentCoreExecutor departmentCoreExecutor;

    /**
     * 查询部门列表
     *
     * @param paging 分页信息
     * @param model 查询条件
     * @return 部门列表
     */
    @ResponseBody
    @RequestMapping("list")
    @RequiresPermissions("personnel:dept:query")
    public ResponseMessage list(DepartmentCoreWhere model, OrderPaging paging, Boolean extra) {
        try {
            PageList<DepartmentCoreBean> list = departmentCoreQueryer.list(model, paging);
            ResponseMessage result = new ResponseMessage(list.toList());
            return result;
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 创建部门 **/
    @ResponseBody
    @RequestMapping("create")
    @RequiresPermissions("personnel:dept:create")
    public ResponseMessage create(DepartmentCoreBean model) {

        try {
            IAccount operator = SessionTools.getLoginUser();
            departmentCoreExecutor.create(model, operator);
            return new ResponseMessage(model);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 修改部门 **/
    @ResponseBody
    @RequestMapping("update")
    @RequiresPermissions("personnel:dept:update")
    public ResponseMessage update(DepartmentCoreUpdate model) {

        try {
            IAccount operator = SessionTools.getLoginUser();
            departmentCoreExecutor.update(model, operator);
            return new ResponseMessage(model);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 删除部门 **/
    @ResponseBody
    @RequestMapping("delete")
    @RequiresPermissions("personnel:dept:delete") // 删除
    public ResponseMessage delete(String[] id) {

        try {
            IAccount operator = SessionTools.getLoginUser();
            departmentCoreExecutor.delete(ConvertTools.toList(id), operator);
            return new ResponseMessage();
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

}
